/*=========================================================================
Copyright 2009 Rensselaer Polytechnic Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 
=========================================================================*/

//#pragma warning(disable:4786)

#include <iostream>
#include <fstream>
#include <list>
#include <queue>
#include <cmath>

#include "CONSTANTS.h"
#include "Mytypes.h"
#include "Config.h"
#include "Cpoints.h"
#include "Dllist.h"
#include "Cimage.h"
#include "C3dimage.h"
#include "Cvector.h"
#include "Cvessel.h"
#include "Template.h"
#include "Extern.h"

#define DISTANCE_THRESHOLD 10

/// used below in GetNumOfOppositPairs
int giMinDirDistance = 3;

//////////////////////////////////////////////////////////////////////////
// Function: FindTwoMaxima
// type: support function for seed point verification
// 
// given the array of boundary points, find two maxima such and point to them
// using the given arguments in the specified order
void FindTwoMaxima(CPoint aPoints[], CPoint** pPoint1, CPoint** pPoint2)
{
	register int i = 0;
	int iIndex1 = 0;
  int iMax1 = 0;
	int iIndex2 = 0;
  int iMax2 = 0;
	int iBefore, iAfter;

	*pPoint1 = 0;
	*pPoint2 = 0;

	for (i = 0; i < NumOfDirections; i++)
	{
		iBefore = i - 1;
		iAfter = i + 1;
		if (i == 0)
			iBefore = NumOfDirections - 1;
		if (i == NumOfDirections - 1)
			iAfter = 0;
		if (aPoints[i].m_iValue > aPoints[iAfter].m_iValue &&
			aPoints[i].m_iValue > aPoints[iBefore].m_iValue)
		{
			if (i < NumOfDirections / 2)
			{
				if (aPoints[i].m_iValue > iMax1)
				{
					iMax1 = aPoints[i].m_iValue;
					iIndex1 = i;
				}
			}
			else
			{
				if (aPoints[i].m_iValue > iMax2)
				{
					iMax2 = aPoints[i].m_iValue;
					iIndex2 = i;
				}
			}
		}
	}

	if (iMax1 && iMax2)
	{
		*pPoint1 = &aPoints[iIndex1];
		*pPoint2 = &aPoints[iIndex2];
	}
}

/////////////////////////////////////////////////////////////////////////
// Function: IsParallel
// type: Support function for seed point verification
//
bool IsParallel(int dir1, int dir2)
{
	if (DirDistance(dir1, dir2) < 3)
		return true;
	else
		return false;
}

/////////////////////////////////////////////////////////////////////////
// Function: IsOpposite
// type: Support function for seed point verification
//
bool IsOpposite(int dir1, int dir2)
{
	int diff = DirDistance(dir1, dir2);
	if (diff >= (NumOfDirections / 4 - 2) && diff <= (NumOfDirections / 4 + 2))
		return true;
	else
		return false;
}

/////////////////////////////////////////////////////////////////////////
// Function: IsClose
// type: Support function for seed point verification
//
bool IsClose(CPoint* pPoint1, CPoint* pPoint2)
{
	int XDiff = pPoint1->m_iX - pPoint1->m_iX;
	int YDiff = pPoint1->m_iY - pPoint1->m_iY;
	int ZDiff = pPoint1->m_iZ - pPoint1->m_iZ;

	int distance = XDiff* XDiff + YDiff* YDiff + ZDiff* ZDiff;

	if (distance < DISTANCE_THRESHOLD)
		return true;
	else
		return false;
}

// Given an array of directions, calculate the number of "almost" opposite
// pairs in the array.
int GetNumOfOppositePairs(int anArray[], int arraySize)
{
	int count = 0;
	int index = - 1;
	int dirDist;
	int minDirDist;
	int tempArray[NumOfDirections];

	memcpy(tempArray, anArray, NumOfDirections * sizeof(int));

	for (register int i = 0; i < arraySize - 1; i++)
	{
		minDirDist = NumOfDirections;

		for (register int j = i + 1; j < arraySize; j++)
		{
			if (tempArray[i] != -1 && tempArray[j] != -1)
			{
				dirDist = abs(DirDistance(anArray[i], anArray[j]) -
						  	NumOfDirections /
						  	2);
				if (dirDist < minDirDist)
				{
					minDirDist = dirDist;
					index = j;
				}
			}
		}

		// if we found one, mark these points as a pair (by deleting them)
		// and increment the count
		if (minDirDist < giMinDirDistance)
		{
			tempArray[i] = -1;
			tempArray[index] = -1;
			count++;
		}
	}
	return count;
}

////////////////////////////////////////////////////////////////////
// Function: ValidSeedCandidate
// CalledBy: FindSeedPoints
// Calls   : -
// Purpose :
//   To avoid having many initial that are adjacent to each other
//
// Logic:
//   Use the 2D array of seed point pointers to check if we already have another
//   seed point in the immediate neighborhood of the given point. If we do, 
//   keep the best one among all of such points
bool AddSeedCandidate(int x, int y, int dir, int value)
{
	int WindowSize = 2;
	bool result = true;

	int giROWS = The3DImage->m_iRows;
	int giCOLS = The3DImage->m_iCols;

	for (register int i = y - WindowSize; i <= y + WindowSize; i++)
	{
		for (register int j = x - WindowSize; j <= x + WindowSize; j++)
		{
			if (i < 0 || i >= giROWS || j < 0 || j >= giCOLS)
				continue;

			// if we already have a point in this window,
			if (gapArrayOfSeedPoints[i][j])
			{
				if (gapArrayOfSeedPoints[i][j]->m_iValue > value)
					result = false;
				else
				{
					delete gapArrayOfSeedPoints[i][j];
					gapArrayOfSeedPoints[i][j] = NULL;
				}
			}
		}
	}

	// if the current point is the best, add it to the array
	if (result)
	{
		gapArrayOfSeedPoints[y][x] = new CPoint(x, y, 0, dir, 0, value);
	}
	return result;
}
////////////////////////////////////////////////////////////
// Function: FindSeedPoints
//
// Find seed points for tracking the given image and store them into
// the given queue. Notice that the seed points need to pass through
// a verification process before they can be considered as valid seed
// points. We do not do this here, since many of such 
// points will not be used, thus avoiding unnecessary computations.
// 
// Logic: seed points are defined as local minima points along 
// horisontal and vertical profiles. To reduce the large number of
// resulting points, only the smallest minima from an interval 
// (i.e. grid_size) is selected.
//
// return the number of points found
//int FindSeedPoints(C3DImage &anImage, CQueue<CPoint> &aQueue, int SliceNum)
int FindSeedPoints2(CImage& anImage)
{
	/*register int x, y, i, max;
	int count = 0;
	unsigned char * *ImageData = anImage.data;*/	
	//10
	//int COL[244] = {38,47,60,62,69,73,75,76,81,86,88,94,101,101,102,107,110,110,111,112,113,116,116,119,119,122,123,126,128,128,131,136,138,138,141,142,144,144,145,145,148,148,151,156,157,159,159,161,161,164,170,172,172,177,179,180,181,182,182,184,187,187,189,189,191,191,192,193,197,199,201,201,202,202,203,205,205,206,206,207,208,209,210,211,214,215,218,218,222,222,224,226,226,231,231,232,233,233,235,236,236,237,243,243,243,243,244,245,245,246,246,247,249,249,250,251,252,253,253,255,255,255,256,257,257,258,258,260,260,260,262,262,263,264,264,265,266,269,270,271,272,274,274,278,278,279,279,284,285,286,286,287,287,288,288,288,289,289,289,289,290,290,290,290,290,291,291,291,292,292,292,293,294,294,295,296,297,298,298,299,301,303,303,306,308,308,312,313,316,318,321,321,325,325,325,328,329,330,331,331,332,332,333,334,334,335,337,337,338,340,342,344,345,346,347,349,350,350,350,353,354,354,355,356,357,358,359,359,360,365,366,374,374,376,378,385,389,391,401,403,404,408,428,432};
	//int ROW[244] = {255,378,378,369,141,239,369,363,293,164,286,358,152,169,300,271,170,283,351,222,177,154,291,164,264,237,174,231,260,344,195,290,201,247,157,231,206,339,230,231,282,299,210,226,158,217,261,309,338,278,158,225,315,267,342,274,329,316,325,162,309,331,228,277,268,323,300,162,287,278,113,128,141,152,233,86,103,86,321,71,255,173,249,79,189,266,72,202,216,319,68,224,225,323,336,350,359,365,68,294,301,113,129,316,327,342,65,206,224,198,217,353,144,189,151,211,159,53,181,164,165,201,40,40,373,377,390,170,193,263,173,399,173,185,245,404,178,414,421,266,441,166,170,245,463,158,462,150,276,276,278,279,281,283,285,288,157,293,304,307,263,290,297,301,308,303,306,312,296,317,321,327,331,336,335,273,127,157,250,116,350,103,129,98,93,235,121,258,154,276,113,114,109,265,283,285,254,288,221,290,279,293,295,143,296,296,296,298,299,302,304,271,307,307,310,139,246,314,315,315,209,321,325,136,331,338,347,351,358,279,376,235,390,131,202,406,292,130,425,225,297,191,305,182};
	//14
	//int COL[676] = {12,17,24,24,25,29,32,37,44,49,50,50,57,58,63,63,65,66,69,70,71,72,78,81,83,84,86,88,89,89,90,93,95,96,98,98,107,109,109,111,112,113,114,119,119,120,124,125,126,128,128,129,130,134,136,136,136,137,139,140,140,142,145,145,145,148,149,149,152,152,152,152,154,155,156,157,158,159,160,163,163,163,164,166,167,168,169,169,170,170,172,174,174,175,175,176,178,179,180,180,180,181,182,185,186,188,189,189,189,189,189,189,189,189,190,191,193,193,193,193,193,194,198,199,200,200,200,200,200,200,200,201,203,203,204,205,206,206,207,207,208,208,208,208,209,209,210,211,212,213,214,214,214,215,215,216,216,217,217,217,218,221,221,221,222,222,222,222,222,223,223,223,223,223,224,224,225,226,227,227,227,227,229,229,231,232,234,235,235,237,237,238,238,239,240,240,241,241,241,241,241,241,242,242,242,242,242,242,244,244,245,246,246,246,247,247,247,253,253,254,254,256,256,258,258,259,261,262,262,262,263,263,264,264,265,265,265,265,266,266,267,267,268,268,269,269,269,269,270,270,271,271,272,272,272,273,273,273,273,274,275,275,275,276,276,277,277,278,278,279,280,280,281,281,281,281,282,282,282,282,283,283,283,283,284,284,285,285,286,286,286,287,287,288,288,288,289,289,289,289,290,290,290,290,290,290,291,291,291,291,292,292,292,293,293,293,294,294,294,295,295,296,296,297,297,298,298,298,298,298,299,299,299,299,300,300,301,301,302,302,302,302,303,304,304,305,305,306,306,307,307,308,308,308,309,309,310,311,311,312,312,313,313,314,314,315,315,316,316,317,317,318,318,318,319,320,321,321,322,323,324,325,325,325,326,326,327,327,328,328,328,329,330,331,331,331,331,332,333,333,334,334,334,335,335,335,336,337,338,338,338,338,339,340,340,341,341,341,341,342,343,344,344,345,345,346,346,347,347,348,348,348,349,350,350,351,351,352,352,353,354,354,355,355,356,356,357,357,357,358,359,360,360,360,361,361,362,362,363,363,364,364,365,366,366,367,367,367,367,368,369,370,370,370,371,372,373,373,374,374,375,375,376,376,376,377,378,379,380,380,381,381,382,382,383,383,384,385,386,386,387,388,389,389,390,391,391,391,391,392,393,393,393,394,395,396,396,397,398,399,399,400,401,402,402,403,404,404,405,406,406,407,407,408,409,409,410,411,412,412,412,413,413,413,414,415,415,416,417,417,418,419,419,420,421,422,422,423,424,425,425,426,427,428,428,429,429,430,431,432,432,433,434,435,435,436,437,438,438,439,440,441,441,442,443,444,445,445,445,446,447,448,448,449,450,451,451,452,453,454,454,455,456,457,458,458,459,460,461,461,462,463,464,464,465,466,467,467,468,469,470,470,471,472,473,474,474,475,476,477,477,478,479,480,480,481,482,483,483,484,485,486,487,487,488,489,490,490,491,492,493,493,494,495,496,496,497,498,499,500,500,501,502,503,503,504,505,506,506,507,508,509,509,510,511,512};
	//int ROW[676] = {109,26,112,386,382,35,388,111,44,391,113,391,113,8,14,54,379,113,113,371,24,24,30,108,59,7,10,207,15,106,36,19,350,106,28,200,108,185,348,63,43,331,180,111,339,339,319,47,335,165,379,378,109,333,52,66,155,52,365,310,365,109,331,350,351,57,83,143,69,302,333,334,4,83,138,93,109,65,321,9,86,93,132,290,74,87,70,93,106,309,80,17,99,50,122,67,78,105,6,87,102,277,26,300,115,106,103,309,318,319,320,328,337,338,23,92,28,73,100,107,270,44,34,9,17,18,110,111,262,277,287,277,29,251,75,59,244,268,45,99,45,114,174,238,161,238,226,152,195,218,54,144,258,72,78,116,138,99,265,276,227,232,240,413,39,68,128,400,401,32,256,398,399,400,272,388,52,84,10,21,349,370,43,248,49,328,278,56,259,247,340,227,288,227,211,212,46,55,64,72,200,344,25,33,34,42,43,138,155,170,9,185,284,348,63,283,284,279,280,67,279,194,353,275,276,79,65,56,358,411,412,413,414,415,48,74,416,417,418,419,420,421,199,422,40,85,423,424,425,426,427,428,89,429,430,80,365,431,432,433,31,434,435,436,437,438,439,440,441,442,443,444,75,193,445,446,85,203,447,448,21,370,449,450,451,452,69,453,59,454,455,456,457,51,458,459,45,72,460,461,30,37,72,187,462,463,66,187,208,464,92,465,466,79,467,468,57,469,470,471,472,473,474,75,475,48,71,84,476,477,85,86,478,479,87,480,88,98,37,38,89,90,91,27,92,93,94,95,397,96,182,97,98,182,99,411,100,101,215,102,103,14,104,105,428,106,107,54,108,109,442,110,111,455,112,113,114,115,116,117,118,66,119,120,121,180,122,391,36,123,124,125,126,61,127,128,219,129,65,130,65,131,132,21,127,133,134,135,10,50,136,137,138,60,139,130,140,141,176,142,143,144,145,64,146,132,147,148,149,58,150,226,151,24,152,153,154,34,155,156,157,158,62,159,12,160,57,161,162,163,164,165,166,403,140,167,168,232,169,170,20,171,172,53,173,53,62,174,175,176,177,50,178,179,180,181,182,183,172,184,144,185,186,187,412,188,189,190,191,192,61,193,194,244,195,196,197,198,199,200,201,202,203,204,205,46,196,206,249,207,62,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,203,223,224,225,226,62,227,228,229,230,231,232,44,233,234,41,235,259,236,237,238,239,212,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,33,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,22,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364};
	//3
	//int COL[124] = {1,9,14,31,32,34,40,43,45,50,53,67,70,72,73,75,76,78,78,86,90,107,112,117,123,139,140,154,156,162,191,204,220,222,231,238,239,241,241,251,255,257,265,271,272,278,281,282,284,291,296,300,305,305,308,309,309,313,315,315,317,318,325,326,327,336,338,347,347,349,352,357,358,359,380,380,389,392,392,392,397,399,399,409,409,414,416,425,426,428,429,439,450,452,455,455,459,472,480,480,484,485,485,486,493,493,493,497,497,498,498,499,500,501,502,503,504,505,507,508,508,508,509,512};
	//int ROW[124] = {176,53,201,69,39,69,59,213,22,77,15,105,154,196,134,256,5,56,223,179,260,259,229,57,267,269,238,269,61,259,279,67,299,373,392,74,441,345,369,317,369,394,453,371,76,333,371,362,402,80,56,457,347,378,24,24,374,406,67,79,88,7,381,460,387,92,385,358,397,410,460,406,390,95,414,467,371,108,399,405,406,419,479,412,490,496,120,398,388,421,429,439,397,130,300,423,450,313,135,404,455,161,328,421,156,320,321,147,295,239,274,410,458,342,256,422,332,221,325,162,314,367,385,349};
	//6
	//int COL[50] = {4,6,8,11,29,45,47,48,57,61,62,76,124,126,145,152,157,162,162,169,184,196,202,207,219,223,234,249,262,270,277,283,293,300,306,319,340,343,363,371,382,393,410,422,422,425,441,457,458,477};
	//int ROW[50] = {303,263,246,284,251,268,225,296,429,324,360,202,484,455,470,140,429,504,505,439,473,448,409,58,18,420,395,400,375,499,383,464,432,388,350,322,329,286,354,242,385,204,428,138,139,452,99,492,54,17};
	//7
	//int COL[186] = {6,10,11,14,16,19,26,30,36,40,52,53,71,75,83,84,88,89,93,109,115,123,123,127,131,138,145,158,160,162,168,168,169,177,185,185,199,199,204,206,212,215,221,222,226,227,227,228,228,229,229,231,231,232,233,233,234,235,237,239,240,241,248,264,265,277,284,285,286,286,290,295,303,303,304,313,318,320,324,328,333,335,337,343,344,355,356,357,360,363,364,368,379,379,387,389,389,392,397,401,403,404,413,414,421,423,434,438,439,443,444,450,451,452,453,453,454,454,455,455,456,457,457,458,459,460,461,462,463,464,465,466,467,468,469,469,469,470,471,472,473,474,475,476,477,478,479,480,480,481,482,482,483,484,485,486,487,488,489,490,491,491,492,493,493,494,495,496,497,498,499,500,501,502,502,503,504,505,506,507,507,508,509,510,511,512};
	//int ROW[186] = {446,468,261,458,233,467,477,240,470,245,243,466,470,478,447,460,231,237,15,69,421,112,235,137,170,398,191,220,220,208,378,417,238,217,243,391,215,367,225,249,231,353,218,255,471,305,471,268,310,238,289,330,455,372,225,432,405,352,249,255,234,211,332,330,202,197,15,193,37,38,307,188,68,298,309,94,170,233,134,293,228,281,208,188,162,137,288,273,152,128,273,187,282,299,265,119,220,272,322,218,254,236,268,267,99,346,259,342,289,388,388,85,86,86,87,271,87,422,88,255,88,89,466,89,90,91,91,92,92,93,93,94,94,95,60,70,95,96,97,97,98,98,99,99,100,100,101,102,269,102,52,103,103,104,104,105,105,106,106,107,108,253,108,58,109,109,110,110,111,111,112,113,113,114,240,114,115,115,116,116,240,117,117,118,119,119};
	//15
	//int COL[165] = {7,8,15,16,17,18,22,22,30,34,37,37,37,45,47,48,52,53,56,56,57,62,63,65,68,70,71,71,75,80,81,84,87,88,92,93,93,93,93,94,95,97,97,101,101,102,104,105,106,107,110,110,114,116,116,116,117,117,122,124,125,126,129,130,133,133,135,135,136,137,137,140,141,141,141,146,149,150,150,150,152,152,154,154,155,156,158,159,161,162,163,166,167,169,171,173,173,174,175,175,176,177,177,178,179,182,184,186,187,190,192,193,203,204,205,206,210,212,216,216,220,223,223,223,225,225,225,227,229,230,233,236,239,239,242,245,246,247,250,253,254,256,256,257,258,259,260,261,263,265,266,266,271,274,279,279,281,288,300,301,304,304,312,320,346};
	//int ROW[165] = {246,259,246,252,346,235,137,329,245,232,118,155,156,166,227,244,316,316,174,331,233,217,178,129,311,200,245,310,310,178,327,245,13,158,15,12,39,90,132,75,52,112,416,34,296,327,143,48,248,398,338,358,63,277,299,339,265,317,75,380,365,140,350,284,87,118,342,412,135,379,443,389,163,334,357,342,283,301,321,322,153,184,123,165,328,144,206,220,185,302,143,164,283,126,331,150,282,272,89,137,152,59,143,348,158,33,285,259,372,294,18,140,309,125,243,397,259,103,54,319,308,34,102,316,364,375,408,236,344,263,327,395,89,312,342,427,332,310,356,346,353,227,450,450,350,314,74,62,383,404,61,369,345,310,377,404,385,301,366,47,432,433,445,379,392};
	//16
	//int COL[201] = {34,50,52,62,75,87,87,92,92,92,92,107,123,141,142,161,162,164,168,169,176,183,183,187,196,202,207,215,216,217,217,217,218,221,222,228,230,230,235,236,238,246,246,246,247,251,256,256,256,258,259,260,261,268,276,277,278,283,284,285,287,294,301,303,303,313,313,314,316,316,324,325,330,330,330,335,335,336,344,345,346,350,350,354,362,364,365,367,367,367,367,368,369,372,379,379,383,384,385,386,387,388,388,389,389,389,394,395,397,398,399,401,402,407,408,408,408,408,409,409,410,412,413,414,417,419,420,420,422,425,425,426,426,429,431,431,433,434,435,436,436,438,438,439,441,442,442,447,447,448,448,448,449,450,453,459,460,460,460,461,461,463,466,467,467,467,472,472,473,474,474,475,476,480,484,484,485,487,487,487,488,490,493,494,498,499,499,500,501,501,501,502,503,503,504,505,506,506,506,508,511};
	//int ROW[201] = {230,319,320,238,340,219,220,163,173,193,194,235,336,237,403,324,414,388,176,144,228,297,377,389,238,360,373,274,274,179,180,181,179,79,240,314,293,294,194,268,87,231,232,282,215,239,264,265,336,230,230,96,269,339,113,255,238,250,226,123,336,227,139,319,336,111,153,221,219,236,310,326,168,228,229,207,208,121,191,302,320,130,239,164,147,319,86,120,121,240,425,59,289,407,43,398,327,344,134,380,291,58,268,259,308,309,384,355,370,132,107,276,276,403,82,124,417,428,391,434,374,352,150,459,345,274,98,332,295,144,178,115,144,286,207,220,308,177,293,138,139,275,276,264,105,149,321,154,265,70,87,284,54,340,35,252,242,281,363,242,294,372,133,290,374,378,381,385,385,122,123,235,252,302,393,396,85,56,106,107,310,232,307,322,367,330,356,254,229,343,344,301,314,317,317,376,226,311,316,321,319};
	//13
	//int COL[350] = {5,7,10,22,24,25,32,33,38,44,46,47,49,51,55,55,57,57,58,62,63,63,63,66,67,68,70,70,71,72,73,75,82,82,82,83,86,90,91,94,97,102,111,112,114,115,116,118,120,121,125,125,127,132,133,133,135,136,137,137,139,139,140,142,144,144,144,145,149,150,150,150,152,153,156,156,158,160,161,162,162,163,164,167,168,168,170,170,171,172,173,173,174,175,176,176,176,178,180,180,181,181,182,182,182,182,184,184,185,186,187,189,189,189,190,191,191,192,193,193,194,195,195,195,195,197,198,198,201,203,203,204,204,205,205,205,205,206,206,207,207,208,208,209,210,210,211,212,214,215,216,217,218,219,219,220,221,221,223,223,223,226,228,231,233,233,234,237,239,240,240,240,240,242,243,244,246,246,248,253,253,256,257,259,259,260,260,262,262,264,264,264,265,265,266,269,272,272,272,273,273,275,279,282,284,284,285,285,285,287,287,288,290,292,294,295,297,298,298,298,298,299,299,300,302,302,303,307,309,311,313,313,313,314,314,315,318,320,321,323,324,324,328,329,332,332,332,333,333,333,334,335,335,337,338,338,339,339,340,341,342,343,343,343,345,345,345,346,348,350,350,351,351,352,352,353,354,354,359,359,361,362,363,363,365,368,368,369,370,370,371,372,374,376,377,382,383,384,384,386,387,389,391,393,394,394,396,399,400,401,402,404,405,406,407,407,412,416,417,418,418,419,419,424,428,429,432,432,435,437,444,444,445,447,448,449,451,454,454,456,457,464,465,465,466,466,475,481,485,497};
	//int ROW[350] = {330,146,248,332,258,163,262,159,219,81,173,73,272,227,103,104,163,335,113,240,120,173,240,155,149,192,110,180,110,169,92,212,127,230,257,280,181,174,328,250,420,149,266,195,160,189,287,331,394,165,196,205,112,274,185,274,375,129,171,209,154,155,175,200,220,290,330,305,362,195,203,204,311,284,301,316,214,308,329,14,355,362,234,21,291,299,227,318,271,308,50,283,341,297,74,223,239,204,65,194,163,293,146,147,162,253,244,328,180,312,102,67,128,334,277,267,295,145,92,254,168,58,200,221,286,200,125,239,74,137,333,93,284,112,320,394,408,101,269,298,376,416,448,448,279,427,279,363,321,321,297,337,351,293,479,260,172,474,247,276,305,227,199,468,456,479,444,273,432,248,292,373,388,410,232,216,198,286,170,130,291,101,416,276,285,65,276,47,309,297,316,317,263,264,420,303,254,255,311,253,254,299,247,285,241,242,274,410,429,257,303,263,285,294,275,316,306,267,268,405,434,259,260,295,311,321,250,294,239,436,278,291,332,228,402,287,334,264,215,208,346,439,253,287,232,275,276,232,243,400,182,221,349,444,282,368,213,286,231,172,257,220,221,371,205,212,396,355,279,192,259,240,285,157,381,190,193,451,184,227,362,243,145,275,176,214,402,460,166,393,364,226,156,200,283,141,468,193,210,270,419,132,178,196,172,476,170,423,122,106,183,165,286,264,95,429,107,261,171,101,292,81,439,256,89,74,80,450,303,248,58,244,64,466,457,50,60,306,473,233,35,24,41,458,224,458,30,218,458,214};
	//WMSVZLexGFAPLaminin2cut-vessel
	//int COL[148] = {18,25,40,40,52,53,54,58,58,73,73,90,103,104,104,106,106,108,110,110,115,115,115,118,119,122,124,124,124,125,126,126,126,127,127,128,128,130,134,134,137,138,138,140,141,142,143,144,144,145,145,146,151,154,160,160,162,162,162,162,167,168,169,169,169,170,170,176,176,179,179,179,185,185,192,192,192,192,198,202,207,207,208,208,212,213,215,215,228,228,229,236,241,242,243,248,249,249,250,256,261,261,263,264,265,267,271,278,282,287,292,295,296,301,304,305,311,314,314,317,318,319,320,322,327,327,328,332,335,337,338,341,342,343,344,344,344,344,346,353,356,356,361,361,368,370,370,371};
	//int ROW[148] = {21,315,290,291,11,11,11,265,266,242,243,215,185,183,184,475,476,491,454,455,438,508,509,421,153,7,595,596,597,131,407,408,415,574,575,520,557,386,397,398,375,99,375,14,14,15,15,15,382,15,382,15,75,360,498,499,591,592,593,594,577,344,478,479,480,59,60,558,559,326,327,328,452,541,49,50,310,515,492,295,469,470,287,288,43,428,275,276,403,404,259,30,386,238,238,371,221,370,369,356,25,198,340,340,339,182,319,308,162,296,142,285,13,124,272,271,254,106,107,242,242,241,11,235,92,93,222,24,76,213,212,206,43,44,45,46,47,48,194,174,161,162,147,148,129,82,83,103};
	//WMSVZLexGFAPLaminin2cut-GFAP
	//int COL[318] = {3,5,6,10,10,11,11,14,14,14,14,18,26,27,27,31,31,34,35,43,43,43,45,48,52,53,53,54,57,58,61,61,62,65,66,67,67,72,74,74,75,75,75,77,82,83,83,87,87,89,89,89,90,91,91,93,94,97,97,99,99,101,104,104,105,105,106,107,108,108,108,108,108,108,110,110,111,111,114,114,114,115,117,119,119,119,120,122,122,125,125,126,126,127,128,130,135,135,136,138,139,142,144,145,145,147,147,147,149,151,153,154,155,155,158,165,165,167,169,170,171,171,172,172,173,173,174,176,177,179,181,182,186,189,189,193,193,195,197,198,199,203,204,206,206,207,209,210,210,211,211,213,215,216,217,219,219,219,219,219,220,220,220,223,223,224,224,224,227,231,232,233,235,235,236,236,237,237,238,241,241,242,243,243,244,246,248,249,251,251,251,253,254,254,255,255,256,256,258,260,262,262,263,263,264,266,268,270,271,273,273,274,276,276,277,278,278,280,282,282,283,283,284,284,284,284,285,286,289,289,289,289,291,293,294,295,296,299,299,300,301,302,304,308,310,310,310,310,310,312,314,314,315,316,318,321,321,322,322,322,323,324,325,326,329,329,329,329,332,332,333,333,336,337,338,339,340,341,343,343,343,344,344,344,345,346,346,350,355,356,356,360,362,365,368,375,375,376,379,379,380,390,392,402,416,422,428,430,432,432,437,442,447,454,455,459,462,462};
	//int ROW[318] = {589,223,99,572,573,176,417,150,228,591,592,555,174,93,410,145,232,13,25,226,430,431,61,20,207,234,446,6,212,226,53,54,219,180,170,170,225,45,181,446,91,104,181,464,26,7,265,9,175,87,184,185,207,34,164,263,178,207,253,197,467,78,90,187,173,187,83,263,205,223,264,502,521,522,76,449,68,550,184,282,283,70,274,203,215,216,568,24,45,146,225,52,53,585,585,347,361,362,156,456,69,432,383,462,463,59,201,397,161,443,420,440,370,371,197,243,244,172,147,433,182,230,42,159,218,344,130,205,195,40,182,436,160,205,206,213,225,195,234,158,382,188,249,335,392,393,380,154,402,386,387,403,378,176,137,216,228,229,385,386,205,409,417,194,336,143,149,414,376,145,137,418,202,220,140,337,337,417,138,205,450,394,135,384,441,134,397,346,129,447,607,436,436,575,129,347,386,592,127,200,379,439,36,123,41,119,27,430,119,119,210,352,116,339,339,114,418,113,112,300,16,359,107,109,320,321,219,105,104,168,180,284,105,104,205,267,101,99,376,246,407,595,96,93,90,91,240,241,502,392,88,409,486,486,87,84,566,417,428,469,82,80,510,79,77,262,435,492,75,210,538,542,73,375,453,198,71,130,65,68,139,8,11,477,522,63,65,62,472,58,503,54,491,52,411,424,472,182,464,482,433,443,476,422,399,387,589,578,375,571,588,601,589,595,595,584,595,596};
	//DAPI_GFAP_Iba1-GFAP
	//int COL[4000] = {1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,6,6,6,6,6,6,6,6,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,12,12,12,13,13,13,14,14,15,15,15,16,16,16,16,16,16,17,17,17,17,17,17,18,18,18,18,19,19,19,19,20,20,20,21,21,21,21,22,22,22,22,22,22,23,23,23,23,23,23,24,24,25,25,25,26,26,26,26,26,27,27,27,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,31,31,31,31,31,32,32,32,32,32,33,33,34,34,34,34,34,34,35,35,35,35,35,35,35,36,36,36,36,36,36,36,36,37,37,37,37,37,38,38,38,38,38,38,38,38,38,39,39,39,39,39,39,40,40,40,40,40,40,40,40,41,41,41,41,41,41,41,41,42,42,42,42,42,42,42,43,43,43,43,43,43,43,43,44,44,44,44,44,44,45,45,45,45,45,46,46,46,46,46,47,47,47,48,48,48,48,49,49,49,49,50,51,51,51,51,51,52,52,52,52,52,52,53,53,53,53,54,54,54,54,54,54,54,54,55,55,55,55,55,56,56,56,56,56,57,58,58,58,58,59,59,60,60,60,60,60,60,60,60,60,60,61,61,61,61,61,61,62,62,62,62,62,62,63,63,63,63,63,63,63,63,63,63,64,64,64,64,64,64,64,65,65,65,65,65,65,65,66,67,67,67,67,67,68,68,68,68,68,68,68,68,68,68,69,69,69,69,69,69,69,69,69,70,70,70,70,70,70,70,70,70,71,71,71,71,72,72,72,73,73,73,73,73,74,74,74,74,74,74,75,75,75,75,75,76,76,76,76,76,76,76,76,77,77,77,77,77,77,77,78,78,78,78,78,79,79,79,79,79,79,79,79,79,79,80,80,80,80,80,80,80,81,81,81,81,81,82,82,82,82,82,82,82,82,82,82,82,83,83,83,83,84,84,84,84,84,85,85,85,85,85,85,85,85,85,85,86,86,86,86,86,86,87,87,87,87,88,89,89,89,89,89,89,90,90,90,90,90,90,91,91,91,91,91,91,91,91,91,92,92,92,92,92,92,93,93,93,93,93,93,93,93,93,93,94,95,95,95,95,95,95,96,96,96,96,96,96,96,97,97,97,98,98,98,98,98,98,98,98,99,99,99,99,99,99,99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,103,103,104,104,104,104,104,104,105,106,106,107,107,107,107,108,108,108,108,108,108,109,110,110,111,111,111,111,111,111,111,112,112,112,112,113,113,113,113,114,114,114,114,114,114,114,115,115,115,116,116,116,117,117,118,118,118,118,118,118,118,118,118,119,119,119,120,120,120,120,120,120,120,121,121,121,121,122,122,122,122,122,123,123,124,124,124,124,124,124,124,125,125,125,125,125,125,125,125,125,125,125,125,126,126,126,126,126,126,126,126,126,126,127,127,127,127,127,127,127,127,127,128,128,128,128,129,129,130,130,130,130,130,130,131,131,131,131,131,131,131,132,132,132,132,133,133,133,133,133,133,134,134,134,135,135,135,135,135,135,135,135,135,136,136,136,136,136,136,136,137,137,137,137,137,137,138,138,138,138,138,139,139,139,139,139,139,139,140,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142,142,142,142,142,142,142,143,143,143,143,143,144,144,144,144,144,144,144,145,145,145,145,145,145,145,145,145,145,145,146,146,146,146,146,147,147,147,147,147,147,148,148,148,148,148,148,149,149,149,149,149,149,149,151,151,151,151,151,151,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153,153,153,153,153,153,154,154,154,155,155,156,156,156,156,156,156,156,157,157,157,157,157,157,158,158,158,158,158,158,158,158,158,158,158,158,159,159,159,160,160,160,160,160,160,160,160,160,160,161,161,161,161,162,162,162,162,162,162,162,162,163,163,163,163,163,163,164,164,164,164,164,164,165,165,165,165,165,165,166,166,166,166,167,167,167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,169,170,170,170,170,172,172,172,172,172,173,173,173,173,173,173,173,173,174,174,174,174,174,174,174,175,175,175,175,175,175,175,176,176,176,176,176,176,176,176,176,177,177,177,178,178,178,178,178,179,179,179,179,180,180,180,180,180,181,181,181,181,181,181,182,182,182,182,182,182,183,183,183,183,183,183,183,184,184,184,184,184,185,185,185,185,185,185,185,186,186,186,186,186,186,187,187,187,187,187,187,187,188,188,188,188,188,189,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191,191,191,191,191,191,191,191,191,192,192,192,193,193,193,193,193,193,193,194,194,194,194,194,194,194,195,195,195,195,195,196,196,196,196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,198,198,198,198,199,199,199,199,199,199,199,199,200,200,200,200,200,200,200,200,200,200,200,200,200,201,201,201,201,201,201,201,201,201,201,201,201,202,202,202,202,203,203,203,203,203,203,204,205,205,205,205,206,206,206,206,207,207,207,207,207,207,208,208,208,208,208,208,208,209,209,209,209,209,210,210,210,210,211,211,211,211,211,211,212,212,212,213,213,213,213,214,214,214,214,214,214,214,215,215,215,215,216,216,216,216,216,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219,219,219,219,219,220,220,220,220,221,221,221,221,222,222,222,223,223,223,224,224,224,224,224,224,224,225,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226,227,227,227,227,227,228,228,228,228,229,229,229,229,229,229,229,230,230,230,231,231,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234,234,234,234,235,235,235,235,235,235,236,236,236,236,236,237,237,237,237,238,238,238,239,239,239,239,239,239,240,240,240,241,241,241,241,242,242,242,242,242,242,242,242,242,242,242,242,243,243,243,243,243,244,244,244,244,245,245,246,246,246,246,246,246,247,247,247,247,247,247,247,248,248,248,248,248,248,248,249,249,249,249,250,250,250,250,250,251,251,251,251,252,252,252,252,252,252,252,253,253,253,253,253,253,254,254,254,254,254,254,254,254,254,255,255,255,255,255,256,256,256,256,256,256,256,257,257,257,257,257,257,258,258,258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,259,260,260,260,260,261,261,261,261,261,262,262,262,262,262,262,262,262,262,263,263,263,263,263,263,263,264,264,264,264,264,264,264,265,265,265,265,266,266,266,267,267,267,268,268,268,268,268,268,268,268,269,269,269,269,269,270,270,270,270,270,271,271,271,272,272,272,273,273,274,274,274,274,274,275,275,276,276,276,277,277,277,277,277,277,277,278,278,278,278,278,279,279,279,279,279,279,279,279,280,280,281,281,282,282,283,283,283,283,283,283,283,283,283,284,284,284,285,285,285,285,285,285,285,286,286,286,286,287,287,287,287,287,287,287,288,288,288,288,289,289,289,289,289,290,290,290,291,291,291,291,292,292,292,292,293,293,293,293,293,294,294,294,294,294,295,295,295,295,296,296,296,297,297,297,298,298,298,300,301,301,301,301,302,302,303,304,305,305,305,305,305,305,305,305,306,306,306,306,307,307,307,307,308,309,309,310,310,310,310,310,311,311,311,313,314,314,314,314,314,315,315,315,315,315,315,316,316,316,316,316,316,317,317,317,318,319,319,319,319,319,319,320,320,320,320,321,321,321,322,322,322,322,323,323,323,323,323,324,324,324,324,324,324,324,324,325,325,325,325,325,325,326,326,326,326,326,327,328,328,328,328,328,328,328,328,328,329,329,329,329,329,330,330,331,331,331,331,331,331,331,331,332,332,332,332,332,332,332,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,335,335,335,335,335,335,335,335,336,336,336,336,337,337,337,338,338,338,338,338,338,339,339,340,340,340,340,340,340,340,341,341,341,341,341,341,342,342,343,344,344,344,344,344,344,344,345,345,345,346,346,347,347,347,347,348,348,348,348,348,349,349,350,350,350,351,351,351,351,351,351,351,352,352,353,353,353,354,354,354,354,355,355,356,356,356,356,356,356,357,357,358,358,358,358,358,358,359,359,359,359,359,359,359,359,360,360,360,360,361,361,361,361,362,362,362,362,362,363,363,364,364,364,364,364,364,364,364,365,366,366,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368,369,369,370,370,370,370,370,371,371,371,371,371,371,371,371,372,372,373,373,373,373,374,374,374,374,375,375,375,376,377,377,377,377,377,377,377,378,378,378,378,379,380,380,380,380,381,381,381,381,382,382,382,382,382,383,384,384,385,385,385,386,386,387,387,387,388,388,388,389,389,389,389,390,390,390,390,391,391,391,392,392,392,392,392,392,392,393,393,393,394,394,394,394,394,394,395,396,396,396,396,396,396,396,397,397,398,399,399,399,399,400,400,400,400,400,400,400,400,401,401,401,401,402,402,402,402,403,403,403,403,403,403,403,403,404,405,405,405,406,406,406,407,407,408,408,408,408,409,409,409,409,409,409,410,410,410,410,410,411,412,412,412,412,412,412,414,414,414,414,416,416,416,417,418,418,418,418,418,419,419,419,420,420,420,420,420,420,421,421,421,422,422,423,424,424,424,424,425,425,425,426,426,427,427,428,428,429,429,429,430,430,430,431,431,431,431,431,431,431,432,432,432,433,434,434,434,434,435,435,435,435,435,435,435,436,436,436,437,437,437,437,438,438,438,438,439,439,439,439,440,440,440,441,442,443,443,443,443,443,443,443,443,444,444,444,445,446,446,446,446,447,447,447,447,448,448,449,449,449,449,449,450,450,450,451,451,451,452,452,454,454,454,454,454,454,454,455,455,455,455,455,456,456,456,456,456,457,457,457,459,459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,461,461,461,462,462,463,463,463,464,464,465,465,465,466,466,466,467,467,467,467,467,467,467,467,467,468,468,468,468,468,469,469,469,469,469,469,470,470,470,471,471,471,471,472,472,472,472,472,472,472,472,472,473,473,473,473,474,474,474,474,474,474,474,474,474,474,475,475,475,475,475,475,476,476,477,477,477,478,478,478,479,479,479,479,479,479,479,479,479,480,480,481,481,481,482,482,482,482,483,483,483,484,484,484,484,484,484,484,484,485,485,486,486,486,487,487,487,488,488,488,488,488,489,489,489,489,489,489,489,489,490,491,491,492,492,492,492,492,492,493,493,493,493,493,493,493,494,494,494,495,495,495,495,496,496,496,497,497,497,497,497,497,497,498,498,498,498,498,498,499,499,499,500,500,500,500,500,500,500,500,500,501,501,501,501,502,502,504,504,504,504,504,505,505,505,505,505,505,505,505,505,506,507,507,507,508,508,508,508,508,508,508,509,509,509,509,509,509,510,511,511,511,511,512,512,512,513,513,513,513,514,514,514,514,514,514,515,515,515,516,516,516,516,516,516,516,516,517,517,517,518,518,518,518,519,519,519,519,520,520,520,520,520,520,520,521,521,521,521,522,522,522,522,522,522,523,523,523,523,523,523,523,523,524,524,524,524,524,524,524,525,525,525,525,525,525,526,526,526,527,527,527,528,528,529,529,530,530,530,530,531,532,532,532,532,533,533,533,533,533,534,534,534,536,536,536,536,537,537,537,537,537,537,537,538,539,539,539,540,540,540,540,540,540,541,542,542,543,543,543,544,544,544,545,545,545,546,546,547,547,547,547,547,548,548,549,549,550,550,551,551,551,552,552,553,554,554,554,554,555,555,555,555,556,556,557,557,558,559,559,559,559,560,561,561,561,561,562,562,562,562,562,562,563,564,564,564,564,564,565,565,565,565,566,566,566,566,566,566,566,567,567,567,568,568,569,569,569,570,570,570,571,571,571,571,572,572,573,573,573,573,573,573,573,574,574,574,574,574,575,575,576,576,576,576,576,578,578,578,578,579,579,579,579,579,579,579,580,580,580,580,580,581,581,581,581,581,581,581,582,582,582,582,582,582,583,583,583,584,584,585,585,585,586,586,588,588,589,589,590,590,590,591,591,592,592,592,592,592,593,593,594,595,595,595,595,596,596,596,596,597,597,597,598,598,598,598,599,600,601,602,602,602,602,603,603,603,603,603,603,603,604,604,604,604,605,605,605,606,606,606,606,607,607,607,608,608,608,608,608,609,609,609,610,610,610,610,611,611,612,612,612,613,613,613,613,613,613,614,614,614,615,615,615,615,615,616,616,616,617,617,617,617,619,619,619,619,620,620,621,621,621,622,623,623,623,624,624,625,626,626,626,626,626,627,627,627,628,628,628,628,629,629,630,630,630,631,631,632,632,633,634,634,635,636,636,636,636,637,637,637,637,638,638,639,639,639,640,640,640,640,640,641,641,642,643,643,643,643,643,643,644,644,644,644,645,646,648,648,648,649,649,649,649,649,649,650,651,652,652,652,653,653,653,653,653,654,654,654,654,654,655,655,655,656,656,656,656,656,656,657,657,657,657,657,657,658,658,658,658,659,659,659,659,659,659,659,660,660,660,661,661,661,662,663,663,663,664,664,664,665,665,665,665,666,666,666,666,666,666,667,667,667,667,667,668,669,669,670,670,670,670,670,670,670,671,671,672,672,672,672,672,673,673,673,673,674,674,675,675,675,675,675,676,676,676,677,678,678,678,678,679,679,679,680,680,680,680,681,681,681,681,682,682,682,682,682,682,683,684,684,684,684,684,685,685,685,685,685,686,686,686,686,687,687,688,688,688,689,689,689,690,690,691,692,692,694,694,695,695,695,695,695,696,696,697,697,697,698,698,698,698,699,699,699,699,699,699,699,700,700,701,701,702,702,702,702,702,702,702,702,703,703,703,703,703,704,704,704,704,704,704,704,705,705,705,706,706,706,706,707,707,708,708,708,708,708,709,709,710,710,710,711,711,711,712,712,713,714,714,714,714,715,715,715,715,715,715,715,715,715,716,716,717,717,717,718,718,719,719,719,719,719,719,720,720,720,721,721,721,722,722,722,723,723,723,724,724,724,724,724,724,725,725,725,725,725,726,726,726,726,726,727,727,728,728,728,728,728,729,729,730,730,730,730,730,731,731,732,733,733,733,733,734,734,735,735,735,735,735,735,736,736,736,736,736,736,737,737,737,738,738,738,738,738,739,740,740,740,741,741,741,741,741,741,741,741,741,742,742,742,742,743,743,743,744,744,745,745,745,745,746,746,746,746,746,747,747,748,748,748,748,748,748,748,748,749,749,749,749,749,750,750,750,750,751,751,751,752,752,753,753,753,753,753,754,754,754,754,754,755,755,755,755,756,756,757,757,757,757,758,758,758,758,758,759,759,759,759,760,760,760,760,760,760,760,760,761,761,761,762,763,763,763,763,763,764,764,764,764,764,764,765,765,765,765,766,766,766,766,766,767,767,768,769,769,769,769,769,770,770,770,770,770,771,771,771,772,772,772,772,772,773,773,773,773,773,774,774,774,775,775,775,775,775,776,776,777,777,777,777,778,778,778,778,779,779,780,780,780,780,780,780,780,780,781,781,781,781,781,782,783,783,783,783,783,784,784,784,784,784,784,785,786,786,786,787,787,787,787,787,787,788,788,788,788,789,789,789,789,789,790,790,790,791,791,791,791,791,791,791,792,792,792,792,792,792,792,793,793,793,793,793,794,794,794,794,795,795,795,796,796,796,797,797,798,798,799,799,799,800,800,800,800,801,801,801,801,801,801,802,802,802,803,803,803,803,804,804,804,805,805,805,805,806,806,806,807,807,807,807,808,808,808,808,808,808,809,809,809,810,810,811,812,812,812,813,813,813,813,813,813,813,814,814,814,814,815,816,816,816,816,816,817,818,818,818,819,820,820,821,822,822,823,824,824,825,825,825,825,826,826,827,829,829,829,829,830,830,830,831,831,831,831,832,832,833,833,833,833,833,833,835,835,835,835,836,836,836,837,837,837,837,838,838,838,838,839,839,841,841,843,844,844,845,845,845,846,846,846,847,847,848,849,849,849,850,851,852,852,853,853,853,853,853,854,854,854,856,856,856,857,857,857,858,859,859,859,859,860,860,860,861,861,861,861,861,862,863,863,864,864,864,864,865,865,866,866,867,867,868,868,869,869,869,870,870,870,870,870,871,872,872,873,873,873,874,877,878,878,878,879,879,881,881,882,882,882,884,885,885,886,886,886,887,887,887,887,888,889,891,891,892,892,893,894,895,895,895,896,896,898,898,898,898,898,901,901,901,901,901,901,901,903,904,904,904,904,905,905,905,905,905,906,907,908,909,909,910,910,914,914,914,916,916,917,917,917,919,919,921,921,922,925,926,926,927,928,928,928,929,929,929,931,931,933,934,934,935,936,936,936,938,940,940,940,941,942,942,942,943,943,944,944,944,946,947,947,948,948,949,950,950,952,952,953,953,953,953,954,954,954,954,954,955,958,958,958,959,960,960,962,962,962,964,964,965,965,965,965,968,968,969,969,971,971,971,971,971,972,972,972,972,972,973,973,973,974,974,974,974,975,976,977,977,978,978,979,979,980,980,981,981,981,981,982,982,984,984,984,985,986,986,986,986,987,987,988,988,988,988,989,989,990,990,990,990,991,991,991,991,991,991,992,992,992,992,993,993,994,995,995,996,996,996,997,998,998,998,999,999,999,999,999,999,1001,1001,1001,1001,1001,1001,1002,1002,1002,1002,1003,1003,1003,1004,1005,1005,1005,1005,1006,1006,1007,1008,1008,1009,1010,1010,1010,1011,1011,1011,1012,1013,1013,1013,1014,1014,1014,1014,1015,1015,1015,1016,1017,1017,1018,1018,1019,1019,1019,1019,1019,1020,1021,1021,1021,1022,1022,1023};
	//int ROW[4000] = {228,279,534,633,658,862,887,354,447,684,862,903,81,222,270,308,402,520,606,629,847,459,606,629,640,669,883,884,823,931,121,226,541,657,658,790,809,880,450,625,857,900,269,462,599,603,658,800,80,277,399,627,632,658,779,802,76,305,496,545,632,643,672,825,184,620,896,943,174,776,851,188,494,626,85,886,230,535,922,400,623,701,856,877,878,238,600,632,790,847,966,95,165,581,646,89,298,887,1018,392,757,870,530,694,861,896,233,699,729,885,901,909,353,490,597,622,831,893,388,745,650,652,735,238,289,719,728,809,787,878,1006,325,537,692,877,878,28,159,576,712,869,901,910,951,241,525,703,44,440,476,725,1010,178,279,366,488,782,881,995,22,240,546,591,820,863,149,331,484,748,764,782,919,29,52,208,236,369,927,963,1001,143,496,575,689,756,182,354,564,574,575,667,856,981,987,270,479,497,555,724,978,187,270,489,490,523,664,789,849,155,200,201,208,646,648,828,879,193,256,570,676,683,966,970,58,259,341,361,484,775,874,966,511,557,567,587,594,995,536,590,664,765,954,118,529,544,944,963,208,730,862,551,648,770,840,479,524,555,897,911,62,159,238,335,1013,85,116,513,691,768,864,658,727,844,1018,46,78,246,368,768,970,995,1006,38,341,474,642,898,55,70,99,736,902,888,379,888,897,898,940,953,95,180,181,185,657,692,726,741,931,969,57,173,259,896,958,969,11,48,115,366,796,896,5,21,27,32,166,167,514,731,828,883,15,102,318,765,848,894,990,69,160,378,738,864,905,923,268,336,441,560,608,891,79,111,153,255,361,408,653,694,745,880,255,373,413,420,726,883,910,964,999,150,273,430,436,826,851,887,983,999,170,764,870,880,62,368,1012,683,725,866,968,974,350,433,648,752,872,898,29,605,795,876,884,21,55,317,335,644,962,980,999,13,90,97,644,682,698,829,5,102,167,341,627,85,209,248,249,560,569,634,639,874,881,438,445,508,718,729,761,869,386,513,573,801,1016,158,215,216,222,239,240,690,760,888,1004,1011,132,351,735,783,48,231,544,696,977,106,146,210,517,704,705,714,737,851,858,40,83,106,252,674,851,29,138,477,889,334,448,661,710,809,965,1015,36,81,131,741,957,971,24,44,456,553,584,790,817,823,981,325,446,717,744,893,992,28,195,320,516,522,725,731,820,845,1000,827,74,248,249,495,900,901,22,67,487,713,891,925,1010,133,591,642,105,106,200,511,527,727,902,903,95,560,611,699,706,718,929,50,129,246,701,837,884,44,141,147,195,696,734,535,649,972,596,692,16,88,318,635,697,896,191,528,914,42,457,732,831,56,628,647,654,900,970,479,103,186,11,81,103,268,520,900,910,77,355,493,875,77,209,611,730,37,53,476,558,576,771,825,183,315,914,276,668,1000,101,582,285,459,662,663,774,831,832,870,1011,499,622,648,200,207,642,648,651,691,823,467,564,610,832,203,484,509,529,834,286,655,61,99,281,321,330,632,863,76,99,159,381,382,688,836,841,867,874,880,913,263,315,563,654,846,851,855,856,860,1013,67,108,152,287,303,339,819,898,983,277,442,676,774,441,467,271,282,518,599,608,861,145,203,308,348,611,815,971,112,262,376,567,356,521,532,563,936,939,92,98,334,197,254,299,365,510,593,625,716,827,118,279,645,750,751,772,861,201,306,515,609,814,861,86,374,380,444,963,41,49,77,143,462,538,811,67,124,129,203,393,520,742,811,95,96,137,138,602,750,826,58,356,386,527,616,750,987,988,153,210,556,591,713,33,336,514,531,637,962,1015,131,146,301,571,604,742,772,808,821,857,976,301,518,540,546,710,49,136,155,305,815,816,127,263,526,596,667,890,93,150,255,282,289,361,970,28,42,141,162,214,600,632,733,771,812,993,1004,1016,24,166,342,439,469,568,953,90,263,299,380,519,554,595,624,706,959,253,274,408,34,947,170,171,305,417,434,515,626,352,448,548,652,884,940,86,146,220,439,448,458,587,773,807,884,930,983,362,953,1017,412,422,423,561,577,669,713,765,891,947,407,571,624,727,149,235,428,466,478,494,649,718,104,273,478,698,824,925,86,154,472,546,620,761,143,263,565,707,830,919,304,414,772,1007,95,414,644,709,888,915,86,248,440,657,754,930,976,359,448,563,585,617,639,690,799,1000,490,528,563,935,16,141,544,722,729,311,504,561,570,709,718,744,912,415,534,645,682,774,885,958,9,23,200,353,458,566,609,112,263,396,451,557,573,634,750,954,653,903,985,562,585,678,699,846,580,703,737,971,438,439,542,655,695,89,204,508,569,603,994,340,388,614,623,674,688,594,678,682,732,749,780,1012,109,530,542,663,718,315,378,516,630,919,924,1001,19,415,564,666,671,931,371,484,491,581,693,745,907,142,498,549,556,937,131,209,357,475,507,567,588,600,662,89,329,333,341,363,435,736,902,51,184,538,543,544,607,631,652,718,852,876,177,671,729,40,269,520,529,570,612,729,127,337,348,642,643,695,919,135,557,623,733,749,58,448,794,841,875,988,84,90,217,541,630,638,674,699,64,74,95,170,506,563,650,843,857,884,912,103,109,110,237,319,457,656,739,13,23,30,117,232,342,404,494,615,662,710,714,717,23,167,366,413,505,515,535,580,637,678,723,755,127,241,242,484,89,223,334,496,705,846,133,164,552,620,886,472,666,849,861,324,489,580,598,867,910,262,407,529,580,593,670,737,9,407,429,618,715,361,456,885,939,42,542,612,847,871,902,604,673,702,589,669,946,980,89,414,531,625,721,724,1017,386,387,844,1017,85,393,465,676,841,374,701,712,740,252,373,577,609,839,894,947,984,37,596,669,681,720,871,885,236,356,614,1017,740,956,1010,1015,18,698,880,40,526,887,40,230,312,365,685,727,1005,35,78,572,589,618,683,809,991,1017,16,67,408,801,946,959,997,422,599,670,787,816,523,732,798,879,9,358,570,685,689,696,697,47,53,889,302,967,28,662,663,682,739,883,942,38,341,408,656,848,854,948,954,14,38,565,677,821,20,213,383,384,514,937,6,297,521,665,1017,397,688,701,860,361,376,889,20,28,368,376,589,1019,479,562,1011,469,559,705,825,11,29,260,491,505,567,735,766,830,840,929,1015,349,459,596,830,994,332,689,749,858,554,982,20,558,755,836,883,1006,350,414,517,599,669,829,842,6,25,274,275,394,758,1001,25,200,911,918,18,424,548,562,968,762,776,851,887,11,16,555,651,750,776,883,20,660,661,842,905,999,284,396,397,398,491,589,694,764,765,543,606,682,776,973,6,399,522,776,968,969,989,330,810,847,921,995,1007,14,186,301,536,554,611,775,795,913,4,12,369,527,668,717,876,883,958,985,603,890,895,928,8,290,291,320,962,621,675,852,885,886,902,968,969,974,106,551,680,743,844,893,974,5,12,384,446,486,587,908,836,857,870,935,128,827,957,138,139,715,10,105,148,375,820,864,889,914,159,308,548,692,920,168,540,584,926,948,416,694,939,381,403,862,752,914,177,573,734,924,925,58,889,714,726,1016,162,530,543,577,791,867,932,697,791,969,989,990,52,587,707,793,870,947,956,978,701,1011,150,564,7,1000,44,286,302,312,381,525,670,755,944,142,936,965,518,686,790,863,866,953,970,134,397,696,761,16,158,273,511,572,679,963,28,150,552,919,128,561,586,735,955,267,268,765,174,575,708,786,100,515,557,939,119,316,317,504,870,5,407,465,776,950,179,407,546,769,144,585,765,157,312,712,10,733,778,446,138,182,404,934,583,607,583,720,19,38,108,136,175,411,540,623,19,610,617,928,328,407,460,601,576,126,782,81,189,444,586,923,90,586,979,608,97,453,532,731,998,49,111,156,573,672,887,16,338,593,602,834,976,172,539,540,924,79,197,439,718,793,848,416,582,857,863,841,857,894,101,591,740,980,7,319,528,537,873,151,165,571,572,580,596,884,1008,173,346,423,566,672,918,94,205,354,546,594,707,77,86,110,528,535,554,575,728,735,266,437,734,735,915,488,549,452,458,493,498,543,544,578,592,13,155,505,537,571,597,696,59,92,103,134,218,471,634,990,465,480,510,534,553,970,1010,123,428,528,564,565,712,892,979,111,487,518,909,146,363,596,131,230,495,503,547,647,198,525,320,465,532,567,632,722,923,252,538,539,640,861,1012,559,867,889,181,242,516,542,647,697,901,467,481,924,86,141,353,366,377,872,541,584,970,977,987,15,563,528,635,709,191,601,683,715,880,956,1008,521,895,251,381,960,428,540,581,889,138,420,327,367,535,597,942,992,546,550,394,510,522,531,669,700,127,420,580,615,624,955,965,986,408,584,709,880,132,182,943,1002,182,436,551,936,974,382,593,104,138,192,363,534,545,607,989,580,417,880,321,376,456,463,469,518,960,968,127,451,575,642,663,691,855,509,624,95,256,408,542,975,46,197,434,435,529,535,697,992,380,872,413,865,896,922,113,620,980,1014,529,534,634,353,108,336,458,567,685,691,959,343,406,541,640,437,41,570,658,1003,92,530,563,611,7,92,112,538,874,372,986,1013,459,903,950,150,523,158,329,978,559,648,968,340,528,642,737,570,789,943,944,658,774,866,41,118,457,520,532,955,962,552,799,962,16,109,396,507,734,861,523,113,386,570,751,760,810,917,760,969,857,528,634,847,934,393,513,519,531,652,745,954,976,468,546,752,989,42,278,493,647,106,219,382,413,465,647,734,842,504,19,833,851,404,469,513,615,703,329,508,622,721,340,495,546,697,921,969,93,413,470,524,949,81,46,71,515,527,758,974,64,519,545,824,266,352,851,113,55,495,526,753,900,498,890,946,48,524,539,616,919,980,586,587,882,9,22,224,37,423,523,819,214,577,872,358,387,129,859,697,879,523,545,949,228,568,575,20,46,139,328,466,559,979,10,459,873,178,53,475,521,856,533,551,557,563,582,754,922,27,218,572,187,422,950,957,223,231,757,968,33,60,61,880,150,687,880,850,210,66,203,357,360,388,523,864,871,513,975,981,923,217,256,538,855,72,217,659,953,361,422,175,349,500,647,987,55,546,554,543,637,676,272,493,62,77,347,513,556,666,818,231,533,576,841,991,75,333,484,921,996,218,513,873,240,360,425,435,473,826,889,68,392,467,531,656,657,855,949,1010,83,267,558,659,991,367,508,562,244,623,643,887,915,376,631,864,73,500,613,651,811,828,897,925,970,30,88,359,442,598,37,276,371,563,590,979,367,423,1018,790,851,858,957,220,393,503,569,621,731,858,943,957,661,798,852,903,24,59,99,451,494,607,635,812,887,903,117,335,456,645,932,933,343,368,271,924,973,160,659,973,18,66,72,665,669,759,790,804,853,34,567,169,814,897,596,606,650,886,188,411,491,103,125,155,177,264,738,759,907,14,398,29,60,207,184,785,812,111,423,601,756,880,376,488,581,663,756,776,782,836,562,168,386,30,56,391,678,705,826,404,502,559,660,782,791,805,482,698,841,8,775,812,991,102,175,1001,21,215,571,752,756,816,822,317,456,484,627,689,756,25,144,654,393,455,606,607,642,664,665,704,710,562,710,796,806,53,752,361,558,564,775,994,195,352,507,597,611,787,817,877,878,108,19,636,658,467,551,667,755,868,889,890,206,484,647,784,827,834,432,321,548,588,778,150,659,772,368,698,763,767,512,579,713,755,885,959,50,686,848,114,382,537,638,676,771,781,822,77,586,893,547,554,952,980,73,527,668,718,12,364,656,681,726,759,875,49,123,805,865,67,384,429,752,753,912,115,376,404,518,576,712,782,792,7,294,651,736,744,997,1009,231,396,717,825,966,989,14,829,888,59,131,372,690,730,548,855,535,567,789,814,117,709,765,845,982,53,553,600,714,747,45,583,925,22,89,142,913,502,691,692,755,756,826,885,692,410,467,836,429,718,724,737,738,764,526,460,969,45,550,783,585,707,926,396,435,770,452,908,34,229,557,957,966,913,932,573,758,539,740,203,524,784,160,192,729,93,388,463,883,42,517,685,691,359,737,705,906,812,54,426,790,799,778,174,717,735,880,86,93,518,685,746,751,918,285,513,710,761,774,169,352,674,892,235,420,461,516,738,901,972,68,190,729,745,936,355,715,754,35,667,798,414,809,956,1010,677,906,112,519,677,725,732,747,913,321,442,507,671,891,61,788,24,70,71,267,686,104,200,459,732,89,104,177,284,702,711,765,190,241,260,803,809,142,359,460,507,672,795,1014,57,507,680,774,898,905,501,755,783,406,727,155,334,704,266,267,102,769,343,666,54,666,837,276,392,69,210,257,437,452,368,772,495,97,260,286,985,150,312,336,708,65,320,679,25,122,724,902,1018,760,460,133,685,1014,1015,268,269,326,709,730,745,978,298,836,906,1006,677,721,836,152,158,443,671,701,702,888,402,698,942,984,1001,277,420,991,110,680,951,988,221,977,64,92,957,24,166,302,477,669,737,148,287,302,247,302,723,964,973,256,828,912,156,271,374,457,324,678,710,954,167,691,80,666,706,402,58,672,722,963,964,911,26,42,187,319,676,26,293,468,18,943,1013,1018,413,722,237,693,732,207,697,485,1007,896,7,189,97,275,663,713,1004,318,384,886,911,301,685,5,526,902,244,281,488,697,707,291,727,473,268,673,712,717,897,975,331,600,692,877,707,684,189,246,909,304,428,701,725,891,967,36,579,59,314,868,195,260,461,476,693,324,664,718,732,885,448,481,685,106,334,685,696,708,958,258,270,673,708,709,900,467,535,673,716,56,77,236,677,725,874,881,182,289,443,857,902,941,432,117,471,944,891,1013,1017,95,279,731,995,76,218,322,462,676,720,37,63,483,712,750,906,244,846,160,202,315,331,340,989,1006,430,470,123,131,267,652,760,172,291,310,715,303,721,50,254,352,353,490,277,657,754,981,27,462,504,998,40,86,323,40,470,705,831,52,248,921,932,52,208,269,845,846,938,357,71,350,676,755,976,340,720,727,814,841,116,258,421,761,116,506,130,698,735,61,179,389,6,757,228,372,968,48,949,27,303,424,665,813,36,676,355,453,754,695,724,733,996,389,661,669,680,746,821,979,926,952,252,327,156,379,664,685,827,855,963,1010,102,156,653,709,710,14,215,341,347,781,879,954,109,705,923,38,393,692,1019,456,827,60,81,193,200,373,341,965,69,354,664,206,237,815,436,946,810,96,216,646,928,109,200,304,646,711,863,885,928,950,9,687,286,377,929,693,962,70,148,875,905,981,993,40,120,340,106,137,315,77,789,831,328,673,926,293,360,367,659,971,1017,348,430,694,800,1009,284,354,391,800,866,420,914,113,322,333,711,839,231,936,89,308,377,378,560,702,904,5,328,417,668,923,182,923,280,343,357,382,647,717,199,647,833,859,945,1006,260,270,319,319,374,407,663,780,319,319,349,806,102,103,245,319,395,424,806,840,943,299,319,657,794,320,353,493,17,320,320,326,758,922,85,320,361,372,637,320,372,93,306,320,400,667,910,936,1013,169,320,490,910,911,320,651,742,743,316,321,568,321,387,228,321,408,732,963,77,321,325,644,732,108,290,321,919,321,382,310,321,552,910,261,322,477,651,673,322,576,662,1018,268,322,368,677,678,848,942,959,285,322,563,322,280,322,543,640,1011,45,238,314,322,705,711,322,664,918,987,323,468,667,911,997,146,323,323,323,579,695,873,1000,323,885,904,959,986,295,323,932,271,282,323,668,911,255,324,330,844,965,262,324,965,297,324,736,879,917,324,829,42,324,568,895,59,60,305,324,96,324,324,673,684,743,770,804,862,983,325,658,684,764,929,325,291,325,627,836,964,309,325,635,707,798,817,325,325,637,809,325,612,809,872,912,981,69,326,895,981,300,326,657,857,888,326,942,993,326,584,613,734,749,942,986,167,290,326,673,679,925,959,326,628,663,877,908,316,326,972,985,600,711,830,42,722,759,202,998,31,286,686,737,954,298,886,913,914,626,672,717,747,854,1006,189,712,846,55,117,310,861,753,875,932,76,726,875,944,521,846,909,5,285,866,920,292,516,517,578,666,879,897,905,1012,692,970,689,278,711,929,4,121,235,307,659,831,958,288,941,949,950,555,190,271,879,917,1012,629,570,615,875,295,679,1005,691,644,963,304,158,977,39,186,672,908,264,625,984,58,104,170,536,692,888,914,182,638,675,907,219,652,603,604,605,626,661,893,172,259,520,660,592,607,793,139,148,543,592,91,92,177,813,113,675,597,654,886,628,668,212,700,786,235,658,684,712,913,109,76,230,864,132,1017,471,655,192,547,615,725,850,203,451,966,769,770,968,85,126,153,24,24,567,761,816,51,211,482,187,609,795,851,912,736,199,566,174,183,455,598,17,964,577,942,96,577,949,950,468,481,552,117,544,545,552,989,668,108,538,141,474,811,687,963,123,449,855,728,833,109,542,96,938,939,904,182,436,446,843,932,199,208,209,951,715,565,896,976,452,854,675,957,34,35,36,97,823,106,142,588,933,960,65,80,89,90,565,728,867,952,122,540,930,985,22,54,100,833,834,662,445,701,139,428,114,737,125,128,947,608,826,584,622,744,540,716,700,762,959,440,35,769,649,837,871,872,597,632,756,573,576,819,697,900,422,26,420,616,767,534,535,712,541,648,696,968,912,974,622,776,957,607,174,723,857,939,592,190,716,419,925,619,787,968,969,656,733,902,985,986,421,626,627,984,748,174,862,159,760,942,558,973,415,791,910,911,130,147,773,900,569,570,622,704,958,580,704,790,907,969,655,864,885,420,632,857,977,234,115,65,849,924,965,30,873,101,810,101,785,906,972,73,408,425,866,877,425,136,425,763,889,210,707,243,864,875,883,743,758,33,353,360,615,132,353,735,787,830,915,353,605,969,994,353,856,353,237,353,5,353,975,354,354,405,633,40,41,344,460,773,908,51,91,128,518,675,1005,16,84,668,982,98,106,661,208,65,501,555,1009,79,444,117,102,869,1019,113,122,760,538,679,714,85,74,520,705,375,383,533,564,517,636,985,928,92,501,83,100,96,495,499,527,539,589,102,659,877,102,114,928};
	//for(i=0;i<4000;i++)
	//{
	//	x = COL[i]-1;
	//	y = ROW[i]-1;
	//	max = ImageData[y][x] + ImageData[y][x - 1] + ImageData[y][x + 1];
	//	if (AddSeedCandidate(x, y, NumOfDirections / 4, max))
	//	{
	//		count++;
	//		// update the average inside pixel and outside pixel
	//		glSumOfSeedPointValues += ImageData[y][x];
	//		glNumOfSeedPoints++;
	//	}
	//}
	//return count;
	

	int giMARGIN2 = 3;
	register int x, y, i;
	register int max, temp, result;
	CPoint aPoint;
	int count = 0;

	unsigned char * *ImageData = anImage.data;

	int giROWS = The3DImage->m_iRows;
	int giCOLS = The3DImage->m_iCols;
	int giGRIDSIZE = gConfig.GetGridSpacing();

	// search along vertical profiles
	// from the left boundary to the right boundary
	for (x = giMARGIN2; x < giCOLS - giMARGIN2; x += giGRIDSIZE)
	{
		// from the top boundary to the bottome boundary
		for (y = giMARGIN2; y < giROWS - giMARGIN2; y++)
		{
			result = 1;
			// the average value of the point (without division)
			max = ImageData[y][x] + ImageData[y][x - 1] + ImageData[y][x + 1];

			// Is this value (i.e. min) the minimum for "HALFGRID" pixels
			// ahead? 
			for (i = y + 1 ; i < (y + giGRIDSIZE); i++)
			{
				if (i >= giROWS - giMARGIN2)
					break;

				// the average of another point along the profile
				temp = ImageData[i][x] +
					ImageData[i][x - 1] +
					ImageData[i][x + 1];

				if (max < temp)
				{
					result = 0;
					break;
				}
			}

			// YES. 
			// add it to the queue and advance x to be HALFGRID points
			// ahead. 
			if (result && max > 3.0 * giMinSeedPixelValue)
			{
				// add the seed point if and only if it satisfies the following
				// test
				if (AddSeedCandidate(x, y, NumOfDirections / 4, max))
					count++;

				// update the average inside pixel and outside pixel
				glSumOfSeedPointValues += ImageData[y][x];
				glNumOfSeedPoints++;

				y += giGRIDSIZE;
			}
			// NO.
			// consider the next point in line
			else
			{
				y = i;
			}
		}
	}

	// search along horizontal profiles.
	// from top boundary to lower one
	for (y = giMARGIN2; y < giROWS - giMARGIN2; y += giGRIDSIZE)
	{
		//////////////////////////////////////////
		// from the left boundary to the right one

		// if the point is a global minimum in the specified interval
		// then add it to the collection of seed points
		for (x = giMARGIN; x < giCOLS - giMARGIN2; x++)
		{
			// if one of the neighboring pixels is set to 255, then it belongs to a
			// a vessel, skip it and a grid size around it
			if (ImageData[y][x] == 255 ||
				ImageData[y - 1][x] == 255 ||
				ImageData[y + 1][x] == 255)
			{
				x += giGRIDSIZE;
				continue;
			}

			result = 1;
			// the average value of the point (without division)
			max = ImageData[y][x] + ImageData[y - 1][x] + ImageData[y + 1][x];

			// Is this value (i.e. min) the minimum for "HALFGRID" pixels
			// to come? 
			for (i = x + 1 ; i < x + giGRIDSIZE; i++)
			{
				if (i >= giCOLS - giMARGIN2)
					break;

				// the average of another point along the profile
				temp = ImageData[y][i] +
					ImageData[y - 1][i] +
					ImageData[y + 1][i];

				if (max < temp)
				{
					result = 0;
					break;
				}
			}

			// YES. 
			// add it to the queue and advance x to be HALFGRID points
			// ahead.
			if (result && max > 3.0 * giMinSeedPixelValue)
			{
				// add the seed point if and only if it satisfies the following
				// test
				if (AddSeedCandidate(x, y, 0, max))
					count++;
				/*
																		CPoint temp(x, y, SliceNum, 0);
																		aQueue.Push(temp);				
																		count++;
														*/				
				// update the average inside pixel and outside pixel
				glSumOfSeedPointValues += ImageData[y][x];
				glNumOfSeedPoints++;

				x += giGRIDSIZE;
			}
			// NO.
			// consider the next point in line
			else
			{
				x = i;
			}
		}
	}

	return count;
}

// these are defined here because MSC++ does not support local functions!
int Average[NumOfDirections];
int compare1(const void* arg1, const void* arg2)
{
	int result = 0;
	if (Average[*(int *) arg1] > Average[*(int *) arg2])
		result = 1;
	else if (Average[*(int *) arg1] < Average[*(int *) arg2])
		result = -1;
	return result;
}
int compare2(const void* arg1, const void* arg2)
{
	int result = 0;
	if (Average[*(int *) arg1] < Average[*(int *) arg2])
		result = 1;
	else if (Average[*(int *) arg1] > Average[*(int *) arg2])
		result = -1;
	return result;
}
//////////////////////////////////////////////////////////////////////////
// Function: VerifySeedPoint
// Input:  A seed point
// Output: Yes/No depending if the point qualifies as an seed point for
//   the tracking process.
//
// Logic:
// 
// A valid seed point is one that:
// 1. Has not been already covered by another tracking cycle.
// 2. Produces two maxima and two minima template responses such that:
//    2.1 the maxima are (almost) opposite to each other, and
//    2.2 the minima are (almost) opposite to the maxima directions.
//		2.3 the maximas and minima are almost perpendicular to each other
// 3. Satisfy minimum response requirements.
//



/////////////////////////////////////////////////////////////////////////////
// Verify that the array of responses (a response for each direction) 
// represents a valid response array according to the old 2D verification rules
bool VerifySeedResponses2D(int* Response, int* dirVotes)
{
	bool result = true;

	//	return true;

	int min[NumOfDirections] =
	{
		0
	};
	int max[NumOfDirections] =
	{
		0
	};

	// variables to be used by subsequent processes
	int dirBefore, dirAfter;

	int minCount = 0, maxCount = 0;

	// weighted average of the responses. This results in responses that
	// are less sensitive to noise. 
	// Notice that we are using the global "Average" array
	int dir, hDir, vDir, hDirBefore, hDirAfter, vDirBefore, vDirAfter;
	int lastIndex = NumOfDirections* NumOfDirections - 1;
	for (register int i = 0; i < NumOfDirections; i++)
	{
		for (register int j = 0; j < NumOfDirections; j++)
		{
			if (i == 0)
				dirBefore = lastIndex;
			if (i == NumOfDirections * NumOfDirections - 1)
				Average[i] = Response[i];

			hDir = i / NumOfDirections;
			vDir = i % NumOfDirections;

			hDirBefore = DirectionMinus(hDir, 1);
			hDirAfter = DirectionPlus(hDir, 1);
			vDirBefore = DirectionMinus(vDir, 1);
			vDirAfter = DirectionPlus(vDir, 1);

			dir = hDir * NumOfDirections + vDir;
			dirAfter = hDirAfter * NumOfDirections + vDirAfter;
			dirBefore = hDirAfter * NumOfDirections + vDirAfter;

			Average[dir] = Response[dirBefore] +
				Response[dirAfter] +
				Response[dir] +
				Response[dir];
		}
	}

	// calculate the number of maxima and minima in "Average"
	for (dir = 0; dir < NumOfDirections; dir++)
	{
		dirBefore = DirectionMinus(dir, 1);
		dirAfter = DirectionPlus(dir, 1);

		if (Average[dir] > Average[dirBefore] &&
			Average[dir] >= Average[dirAfter])
		{
			max[maxCount] = dir;
			++maxCount;
		}
		else if (Average[dir] < Average[dirBefore] &&
			Average[dir] <= Average[dirAfter])
		{
			min[minCount] = dir;
			++minCount;
		}
	}

	////////////////////////////////////////
	// 2. Do we have at least two maxima and two minima?
	if (maxCount < 2)//|| minCount < 2)
	{
		result = false;
	}
	else
	{
		// sort them so that the valid responses are listed first
		qsort((void *) max, maxCount, sizeof(int), compare2);
		qsort((void *) min, minCount, sizeof(int), compare1);

		int max0 = Average[max[0]];
		int max1 = Average[max[1]];
		int min0 = 2 * Average[min[0]];
		int min1 = 2 * Average[min[1]];

		// the threshold is set based on the minimum detectable edge.
		// for a template of length 12
		if (min0 > max0 || min0 > max1 || min1 > max0 || min1 > max1)
			result = false;


		/////////////////////////////////////////////////////////////
		// 3. all the above conditions were satisfied. perform the last
		//    threshold test
		if (result)
		{
			// if the number of maximas (minimas) is not even, only use those
			// the strongest (weakest) responses.
			if (maxCount % 2 != 0)
				maxCount--;

			if (minCount % 2 != 0)
				minCount--;

			// 2.1 If we have two maximas, then we expect them to be almost opposite
			//     If we have four maximas, then we expect to have two pairs of almost
			//     opposite points, so perform such check
			//
			// count how many opposite pairs we have in each array
			int NumOfOppositePairs = GetNumOfOppositePairs(max, maxCount);
			if (NumOfOppositePairs < 1)//(maxCount/2))
				result = false;

			//
			// vote for the desired initial direction
			if (result)
				dirVotes[max[0]]++;
		}
	}
	return result;
}

deque<CPoint> boundaries;

/////////////////////////////////////////////////////////////
// Function: VerifySeedPoint3D2
//
// Verify that the given point is a valid seed point.
bool VerifySeedPoint3D2(CPoint* aPoint)
{
	bool result = true;

	/////////////////////
	// Notice that the given point does not have its z-component assigned yet
	// becuase it was obtained from the projection image. So find the plane
	// with the highest average and assign it as the possible z-value
	int y = aPoint->m_iY;
	int x = aPoint->m_iX;
	int z = 0;
	int sum, Max = 0;
	
	int giSLICES = The3DImage->m_iSlices;
	register int i;
	for (i = 0; i < giSLICES; i++)
	{
		sum = The3DImage->data[i][y - 1][x - 1] +
			The3DImage->data[i][y - 1][x] +
			The3DImage->data[i][y - 1][x + 1] +
			The3DImage->data[i][y][x - 1] +
			The3DImage->data[i][y][x] +
			The3DImage->data[i][y][x + 1] +
			The3DImage->data[i][y + 1][x - 1] +
			The3DImage->data[i][y + 1][x] +
			The3DImage->data[i][y + 1][x + 1];

		if (sum > Max)
		{
			Max = sum;
			z = i;
		}
	}

	aPoint->m_iZ = z;
	
	

	// an array of votes for each direction
	//int HDirVotes[NumOfDirections];
	int* DirVotes = new int [NumOfDirections* NumOfDirections];
	int* Response = new int [NumOfDirections* NumOfDirections];
	//memset(HDirVotes, 0, sizeof(int)*NumOfDirections);
	memset(DirVotes, 0, sizeof(int) * NumOfDirections * NumOfDirections);
	memset(Response, 0, sizeof(int) * NumOfDirections * NumOfDirections);

	// since we call this function before we perform any tracking, 
	// we don't have to see if the candidate point lies on an already 
	// tracked vessel

	CPoint tempPoint(*aPoint);
	CPoint HLeftPoint, HRightPoint;
	CPoint VLeftPoint, VRightPoint;
	CPoint BestHLeftPoint, BestVLeftPoint;
	CPoint BestHRightPoint, BestVRightPoint;

	int BestResponse = 0;
	int index = 0;

	int BestForegroundEstimate = 0;
	int BestBackgroundEstimate = 0;

	int VDirections[9];
	PrepareDirectionsArray(VDirections, 9, 0);
	register int Hdir, Vdir, v;

	// notice that since we limit starting points to those withing +/- 45
	// vertical degrees. This usually produces more stable tracking results.

	// for each possible direction, record the maximum left and right
	// template responses in that direction
	for (Hdir = 0; Hdir < NumOfDirections; Hdir++)
	{
		for (v = 0; v < 9; v++)
		{
			Vdir = VDirections[v];
			Response[index] = gHLeftTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
																	& HLeftPoint) +
				gHRightTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
												   	& HRightPoint) +
				gVLeftTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
												  	& VLeftPoint) +
				gVRightTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
												   	& VRightPoint);

			if (Response[index] > BestResponse)
			{
				BestResponse = Response[index];
				BestHLeftPoint = HLeftPoint;
				BestHRightPoint = HRightPoint;
				BestVLeftPoint = VLeftPoint;
				BestVRightPoint = VRightPoint;

				BestForegroundEstimate = gHLeftTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue +
					gHRightTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue +
					gVLeftTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue +
					gVRightTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue;

				BestBackgroundEstimate = gHLeftTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue +
					gHRightTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue +
					gVLeftTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue +
					gVRightTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue;
			}
			index++;
		}
	}

	BestForegroundEstimate /= 4;
	BestBackgroundEstimate /= 4;

	int iContrastThresholdMultiplier = gConfig.GetContrastThresholdMultiplier();
	int iUsedTemplateLength = gConfig.GetMinimumTemplateLength();
	
	double threshold = 0;
	if (gf3DStdDev > 3.0)
		threshold = 3 * 3 * iUsedTemplateLength;
	else if (gf3DStdDev > 0.0)
		threshold = gf3DStdDev * 3.0 * iUsedTemplateLength;
	else
		threshold = 3.0 * iUsedTemplateLength;

	threshold *= iContrastThresholdMultiplier;

	// we assume starting points must have at least a contrast of 3
	if (BestResponse < threshold)
		result = false;

	if (BestHLeftPoint.m_iValue < threshold ||
		BestHRightPoint.m_iValue < threshold ||
		BestVRightPoint.m_iValue < threshold ||
		BestVLeftPoint.m_iValue < threshold)
		result = false;

	if (result /*&& VerifySeedResponses2D(Response, DirVotes)*/)
	{
		aPoint->m_iValue = BestResponse / 4;

		aPoint->m_iHDir = BestHRightPoint.m_iHDir;
		if (BestHLeftPoint.m_iValue > BestHRightPoint.m_iValue)
			aPoint->m_iHDir = BestHLeftPoint.m_iHDir;

		aPoint->m_iVDir = BestVRightPoint.m_iVDir;
		if (BestVLeftPoint.m_iValue > BestVRightPoint.m_iValue)
			aPoint->m_iVDir = BestVLeftPoint.m_iVDir;

		GetCenterLocation(*aPoint,
			BestHRightPoint,
			BestHLeftPoint,
			BestVRightPoint,
			BestVLeftPoint);

		aPoint->m_iValue = BestResponse;

		double Xdiff = BestHLeftPoint.m_iX - BestHRightPoint.m_iX;
		double Ydiff = BestHLeftPoint.m_iY - BestHRightPoint.m_iY;
		double Zdiff = BestHLeftPoint.m_iZ - BestHRightPoint.m_iZ;

		double width = std::sqrt(Xdiff* Xdiff + Ydiff* Ydiff + Zdiff* Zdiff);

		// the following two lines were added for CANCER images
		double h_width = BestHLeftPoint.FindDistance(&BestHRightPoint);
		double v_width = BestVLeftPoint.FindDistance(&BestVRightPoint);
		gfHWidth += (float)h_width;
		gfVWidth += (float)v_width;
		aPoint->m_fHWidth =(float) h_width;
		aPoint->m_fVWidth = (float)v_width;


		gfWidthSum +=(float) width;
		giNumOfWidthSumMembers++;
	}
	else
		result = false;

	// 1. if the point is a valid point, update the foreground and background histograms
	// 2. add the point to the list of seed points
	if (result == true)
	{
		deque<CPoint> points; 
		points.push_back(*aPoint);
		points.push_back(BestHRightPoint);
		points.push_back(BestHLeftPoint);
		points.push_back(BestVRightPoint);
		points.push_back(BestVLeftPoint);
		Seeds.push_back(points);

		gaiForegroundHistogram[BestForegroundEstimate]++;
		gaiBackgroundHistogram[BestBackgroundEstimate]++;
	}

	delete [] Response;
	delete [] DirVotes;

	return result;
}

bool VerifySeedPoint3D3(CPoint* aPoint)
{
	bool result = true;

	/////////////////////
	// Notice that the given point does not have its z-component assigned yet
	// becuase it was obtained from the projection image. So find the plane
	// with the highest average and assign it as the possible z-value
	int y = aPoint->m_iY;
	int x = aPoint->m_iX;
	int z = 0;
	int sum, Max = 0;
	
	int giSLICES = The3DImage->m_iSlices;
	register int i;
	for (i = 0; i < giSLICES; i++)
	{
		sum = The3DImage->data[i][y - 1][x - 1] +
			The3DImage->data[i][y - 1][x] +
			The3DImage->data[i][y - 1][x + 1] +
			The3DImage->data[i][y][x - 1] +
			The3DImage->data[i][y][x] +
			The3DImage->data[i][y][x + 1] +
			The3DImage->data[i][y + 1][x - 1] +
			The3DImage->data[i][y + 1][x] +
			The3DImage->data[i][y + 1][x + 1];

		if (sum > Max)
		{
			Max = sum;
			z = i;
		}
	}

	aPoint->m_iZ = z;	

	int Response;

	CPoint tempPoint(*aPoint);
	CPoint HLeftPoint, HRightPoint;
	CPoint VLeftPoint, VRightPoint;
	CPoint BestHLeftPoint, BestVLeftPoint;
	CPoint BestHRightPoint, BestVRightPoint;

	BestHLeftPoint.m_iValue = BestHRightPoint.m_iValue = 0;
	BestVLeftPoint.m_iValue = BestVRightPoint.m_iValue = 0;
	int BestResponse = 0;

	int giShiftDistance = gConfig.GetMaximumShiftDistance();
	int ShiftDistance = giShiftDistance;
	
	int BestForegroundEstimate = 0;
	int BestBackgroundEstimate = 0;

	int VDirections[9];
	PrepareDirectionsArray(VDirections, 9, 0);
	register int Hdir, Vdir, v;

	// notice that since we limit starting points to those withing +/- 45
	// vertical degrees. This usually produces more stable tracking results.

	// for each possible direction, record the maximum left and right
	// template responses in that direction
	for (Hdir = 0; Hdir < NumOfDirections; Hdir++)
	{
		for (v = 0; v < 9; v++)
		{
			Vdir = VDirections[v];
			Response = gHLeftTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
														 	& HLeftPoint) +
				gHRightTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
												   	& HRightPoint) +
				gVLeftTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
												  	& VLeftPoint) +
				gVRightTemplatesArray[Hdir][Vdir]->CalculateMaxResponse(&tempPoint,
												   	& VRightPoint);

			if (Response > BestResponse)
			{
				BestResponse = Response;
				BestHLeftPoint = HLeftPoint;
				BestHRightPoint = HRightPoint;
				BestVLeftPoint = VLeftPoint;
				BestVRightPoint = VRightPoint;

				BestForegroundEstimate = gHLeftTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue +
					gHRightTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue +
					gVLeftTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue +
					gVRightTemplatesArray[Hdir][Vdir]->m_iForegroundPixelValue;

				BestBackgroundEstimate = gHLeftTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue +
					gHRightTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue +
					gVLeftTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue +
					gVRightTemplatesArray[Hdir][Vdir]->m_iBackgroundPixelValue;
			}
		}
	}

	BestForegroundEstimate /= 4;
	BestBackgroundEstimate /= 4;

	int giUsedTemplateLength = gConfig.GetMinimumTemplateLength();
	double threshold = 0;
	if (gf3DStdDev > 3.0)
		threshold = 3 * 3 * giUsedTemplateLength;
	else if (gf3DStdDev > 0.0)
		threshold = gf3DStdDev * 3.0 * giUsedTemplateLength;
	else
		threshold = 3.0 * giUsedTemplateLength;

	// we assume starting points must have at least a contrast of 3
	if (BestResponse < threshold)
		result = false;

	if (BestHLeftPoint.m_iValue < threshold ||
		BestHRightPoint.m_iValue < threshold ||
		BestVRightPoint.m_iValue < threshold ||
		BestVLeftPoint.m_iValue < threshold)
		result = false;

	// revert to original value
	giShiftDistance = ShiftDistance;

	if (result)
	{
		aPoint->m_iValue = BestResponse / 4;
		aPoint->m_iHDir = BestHRightPoint.m_iHDir;
		if (BestHLeftPoint.m_iValue > BestHRightPoint.m_iValue)
			aPoint->m_iHDir = BestHLeftPoint.m_iHDir;

		aPoint->m_iVDir = BestVRightPoint.m_iVDir;
		if (BestVLeftPoint.m_iValue > BestVRightPoint.m_iValue)
			aPoint->m_iVDir = BestVLeftPoint.m_iVDir;

		// fix the position of the starting point
		aPoint->m_iX = (int)
			((BestHLeftPoint.m_iX + BestHRightPoint.m_iX) / 2.0 + 0.5);
		aPoint->m_iY = (int)
			((BestHLeftPoint.m_iY + BestHRightPoint.m_iY) / 2.0 + 0.5);
		aPoint->m_iZ = (int)
			((BestVLeftPoint.m_iZ + BestVRightPoint.m_iZ) / 2.0 + 0.5);

		int Xdiff = BestHLeftPoint.m_iX - BestHRightPoint.m_iX;
		int Ydiff = BestHLeftPoint.m_iY - BestHRightPoint.m_iY;
		int Zdiff = BestHLeftPoint.m_iZ - BestHRightPoint.m_iZ;

		float width = sqrt(static_cast<float>(Xdiff* Xdiff + Ydiff* Ydiff + Zdiff* Zdiff));

		gfWidthSum += width;
		giNumOfWidthSumMembers++;
	}
	else
		result = false;

	// if the point is a valid point, update the 
	// foreground and background histograms
	if (result == true)
	{
		gaiForegroundHistogram[BestForegroundEstimate]++;
		gaiBackgroundHistogram[BestBackgroundEstimate]++;
	}

	return result;
}

bool SearchForSeedAlongA_Line(CPoint& aPoint, CVector* aVector,
	CPoint& minPoint)
{
	cout <<
		"This function : SearchForSeedAlongA_Line Should've not been called "
		<<
		endl;
	exit(0);
	return false;
	/* 3-3-1999
	bool result = false;
	int min, temp;
	// determine that the whole line is within the boundaries of the image 
	// being tracked (i.e. TheTargetImage) 
	int x1 = aPoint.m_iX + aVector->IndexC[0];
	int x2 = aPoint.m_iX + aVector->IndexC[MaxVectorLength-1];
	int y1 = aPoint.m_iY + aVector->IndexR[0];
	int y2 = aPoint.m_iY + aVector->IndexR[MaxVectorLength-1];
	if( (x1 > giMARGIN && x1 < giCOLS-giMARGIN) &&
		 (x2 > giMARGIN && x2 < giCOLS-giMARGIN) &&
		 (y1 > giMARGIN && y1 < giROWS-giMARGIN) &&
		 (y2 > giMARGIN && y2 < giROWS-giMARGIN) )
	{
		// the orthogonal directions
		int dir1 = DirectionPlus(aPoint.dir, NumOfDirections/4);
		int dir2 = DirectionMinus(aPoint.dir, NumOfDirections/4);
		// a pointer to the image pixel where the vector is starting
		unsigned char *baseData  = &(The3DImage->data[aPoint.m_iZ][y1][x1]);
		unsigned char *baseData1 = baseData + VectorsArray[dir1]->data[1];
		unsigned char *baseData2 = baseData + VectorsArray[dir2]->data[1];
		for(register int i = 0; i < MaxVectorLength; i++)
		{
			result = true;
			min    = *(baseData  + aVector->data[i]) + 
					  *(baseData1 + aVector->data[i]) +
					  *(baseData2 + aVector->data[i]);
			// Is this value (i.e. min) the minimum until the end of the line
			// ahead? 
			for(register int j = i + 1 ; j < MaxVectorLength; j++)
			{
				// the average of another point along the profile
				temp = *(baseData  + aVector->data[j]) + 
						*(baseData1 + aVector->data[j]) +
						*(baseData2 + aVector->data[j]);
				if(min > temp)
				{
					result = false;
					break;
				}
			}
				
			// YES. 
			// add it to the queue and advance x to be HALFGRID points
			// ahead. 
			if(result)
			{
				minPoint.m_iX = aPoint.m_iX + aVector->IndexC[i];
				minPoint.m_iY = aPoint.m_iY + aVector->IndexR[i];
				minPoint.m_iZ = aPoint.m_iZ;
				minPoint.dir = aPoint.dir;
				break;
			}
			// NO.
			// consider the next point in line
			else
			{
				i = j;
			}
		}
	}
	return result;
	*/
}

///////////////////////////////////////////////////////////////////////////////
// Function: FindAndAddMoreSeedPoints
//
// Purpose: 
//		To locate potential seed points that are encountered while tracking
// Logic:
//		Starting from the given point, search along the two lines extending in 
//  	the opposite directions for one local minima to be considered later as a
//		seed point candidate. Such point must pass through the verification 
//		process before it can be used as a seed in the tracking algorithm
//
/*
void FindAndAddMoreSeedPoints(CPoint &aPoint)
{
	// two vectors, one for each direction
	CVector *Vector1 = VectorsArray[aPoint.dir];
	CVector *Vector2 = VectorsArray[DirectionPlus(aPoint.dir, NumOfDirections/2)];
	
	// a point to hold the seed candidate
	CPoint result(0,0,0,0);
	// Search for the seed in one direction. The result is stored in (result)
	if(SearchForSeedAlongA_Line(aPoint, Vector1, result))
	{
		InitialPoints.Push(result);
		cout << "\n Initial Point Pushed: ";
		BranchingPointsImage->MarkPoint(&result, 254);
		//result.Print();
		//WorkingImage->MarkPoint(&result, 250);
	}
	// do the same for the other direcion
	if(SearchForSeedAlongA_Line(aPoint, Vector2, result))
	{
		InitialPoints.Push(result);
		cout << "\n Initial Point Pushed: ";
		BranchingPointsImage->MarkPoint(&result, 254);
		//result.Print();
		//WorkingImage->MarkPoint(&result, 250);
	}
}
*/

int compare5(const void* arg1, const void* arg2)
{
	int result = 0;
	int value1 = (*(CPoint**) arg1)->m_iValue;
	int value2 = (*(CPoint**) arg2)->m_iValue; 
	if (value1 < value2)
		result = 1;
	else if (value1 > value2)
		result = -1;
	return result;
}
void SortSeedPoints()
{
	register int Index = 0;
	int giROWS = The3DImage->m_iRows;
	int giCOLS = The3DImage->m_iCols;

	// fill the sorted array of pointers
	for (register int i = 0; i < giROWS; i++)
	{
		for (register int j = 0; j < giCOLS; j++)
		{
			if (gapArrayOfSeedPoints[i][j])
			{
				gapSortedArrayOfSeedPoints[Index] = gapArrayOfSeedPoints[i][j];
				Index++;
			}
		}
	}

	giNumOfSeedPoints = Index;
	qsort((void *) gapSortedArrayOfSeedPoints,
		giNumOfSeedPoints,
		sizeof(CPoint *),
		compare5);
}

void VerifyAndSortAllSeedPoints()
{
	register int HDir, VDir;

	for (HDir = 0; HDir < NumOfDirections; HDir++)
	{
		for (VDir = 0; VDir < NumOfDirections; VDir++)
		{
			gHLeftTemplatesArray[HDir][VDir]->AssociateWithImage(*The3DImage);
			gHRightTemplatesArray[HDir][VDir]->AssociateWithImage(*The3DImage);
			gVLeftTemplatesArray[HDir][VDir]->AssociateWithImage(*The3DImage);
			gVRightTemplatesArray[HDir][VDir]->AssociateWithImage(*The3DImage);
		}
	}

	CImage tempImage(*ProjectionImage);

	register int i, j;
	register int iIndex = 0;
	register CPoint * pPoint = NULL;
	register double dArea = 0;
	list<deque<CPoint> >::iterator k;

	int giROWS = The3DImage->m_iRows;
	int giCOLS = The3DImage->m_iCols;
	for (i = 0; i < giROWS; i++)
	{
		for (j = 0; j < giCOLS; j++)
		{
			pPoint = gapArrayOfSeedPoints[i][j];
			if (pPoint)
			{
				UnverifiedSeeds.push_back(*pPoint);
				if (VerifySeedPoint3D2(pPoint))
				{
					tempImage.data[pPoint->m_iY][pPoint->m_iX] = 254;
					pPoint->m_iValue = pPoint->m_iValue * ((int)
						(The3DImage->data[pPoint->m_iZ][pPoint->m_iY][pPoint->m_iX]));

					gapSortedArrayOfSeedPoints[iIndex] = pPoint;
					iIndex++;
					dArea += pPoint->m_iValue;
					VerifiedSeedsCenter.push_back(*pPoint);
				}
				else
				{
					tempImage.data[pPoint->m_iY][pPoint->m_iX] = 255;
					delete gapArrayOfSeedPoints[i][j];
					gapArrayOfSeedPoints[i][j] = NULL;
				}
			}
		}
	}

	giNumOfSeedPoints = iIndex;
	qsort((void *) gapSortedArrayOfSeedPoints,
		giNumOfSeedPoints,
		sizeof(CPoint *),
		compare5);
	Seeds.sort();

	VerifiedSeedsCenter.clear();
	list<deque<CPoint> >::reverse_iterator tp;
	
	CPoint Seed;
	for (tp = Seeds.rbegin(); tp != Seeds.rend(); tp++) {
		Seed = (*tp)[0];
		VerifiedSeedsCenter.push_back(Seed);
	}

	/*
		// output the points
		ofstream outFile("Points.txt");
		for(i = 0; i < giNumOfSeedPoints; i++)
			outFile << gapSortedArrayOfSeedPoints[i]->m_iValue << endl;
		register double dArea2 = 0;
		for( i = 0; i < giNumOfSeedPoints; i++) {
			
			dArea2 += gapSortedArrayOfSeedPoints[i]->m_iValue;
			// include all seeds points comprising 90% of the area
			if((dArea2/dArea) > 0.95) {
				cout << "\nThrshold: " << gapSortedArrayOfSeedPoints[i]->m_iValue << endl;
				break;
			}
			tempImage.data[gapSortedArrayOfSeedPoints[i]->m_iY][gapSortedArrayOfSeedPoints[i]->m_iX] = 254;
		}
		for(j = i; j < giNumOfSeedPoints; j++) {
			tempImage.data[gapSortedArrayOfSeedPoints[j]->m_iY][gapSortedArrayOfSeedPoints[j]->m_iX] = 255;
			giNumOfSeedPoints--;
			delete gapSortedArrayOfSeedPoints[j];
			gapSortedArrayOfSeedPoints[j] = NULL;
		}
	*/	
	//	cout << "\n\n A total of : " << giNumOfSeedPoints << " were found.\n"
	//		  << endl;

	//	tempImage.Write("AllSeeds.pgm");
	//exit(0);
}


